package com.skhu.as.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.skhu.as.domain.DutyHours;
import com.skhu.as.service.DutyHoursS;

public class DutyHoursC extends SimpleFormController {

	private DutyHoursS dutyHoursS;

	public void setDutyHoursS(DutyHoursS dutyHoursS) {
		this.dutyHoursS = dutyHoursS;
	}
	
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {

		if(!isFormSubmission(request)){
			boolean def = ServletRequestUtils.getBooleanParameter(request, "default", false);
			int result = 0;
			
			if(def){
				result = dutyHoursS.deleteAllDutyHours();
			}
		}
		
		return super.formBackingObject(request);
	}
	
	@Override
	protected Map referenceData(HttpServletRequest request) throws Exception {
		List<DutyHours> dutyHourslist = dutyHoursS.getDutyHoursList();
		Map ref = new HashMap();
		ref.put("dutyHourslist", dutyHourslist);
		
		return ref;
	}
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		int dutyHoursNo = dutyHoursS.getDutyHoursMaxNo();
		String dutyHoursString = ServletRequestUtils.getStringParameter(request, "dutyHoursString", "");
		
		dutyHoursS.insertDutyHours(dutyHoursNo + 1, dutyHoursString);
		
		return super.onSubmit(request, response, command, errors);
	}
	
}
